﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.IO;
using Common;

public partial class website_admin_members_shippingaddresslist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "loadingdata":
                LoadingData();
                break;
            case "getregionlist":
                LoadingRegionList();
                break;
        }
    }

    #region 信息绑定
    void LoadingData()
    {
        string ctrl = "~/website_admin/members/_controls/ctlMemberList.ascx";

        string SQL = " ";

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("username")))
        {
            SQL += " AND (UserName LIKE '%" + RequestBase.GetFormString("username") + "%' OR FullName LIKE '%" + RequestBase.GetFormString("username") + "%')";
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("regionnumber")))
        {
            SQL += " AND RegionNumber LIKE '" + RequestBase.GetFormString("regionnumber") + "%'";
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("address")))
        {
            SQL += " AND ShipAddress LIKE '%" + RequestBase.GetFormString("address") + "%'";
        }

        Control uc_rep = LoadControl(ctrl);

        Repeater rptShippingAddressList = (Repeater)uc_rep.FindControl("rptShippingAddressList");

        DBUtility.Pagination pagination = new DBUtility.Pagination();
        try
        {
            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = " ORDER BY ShipAddressName ASC";
            pagination.Primarykey = "ShippingAddressId";
            pagination.SearchField = "*";
            pagination.TableName = "vw_ShippingAddressBase";
            pagination.PageSize = 30;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL.Trim();

            rptShippingAddressList.DataSource = pagination.GetDataTable();
            rptShippingAddressList.DataBind();

            pagination.PageBarFerrari();
            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptShippingAddressList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 获取三级地区级联目录数据
    /// <summary>
    /// 获取三级地区级联目录数据
    /// </summary>
    void LoadingRegionList()
    {
        string parentid = RequestBase.GetFormString("parentid");
        StringBuilder selOptions = new StringBuilder();
        string items = "<option value=\"{0}\">{1}</option>";
        string SQL = "";

        string layer = RequestBase.GetFormString("layer");
        switch (layer)
        {
            case "1":
                selOptions.Append(string.Format(items, "", "-- 请选择省份 --"));
                SQL = " AND DeletionStateCode = 0 AND Layer=" + layer + " ORDER BY RegionName ASC";
                break;
            case "2":
                selOptions.Append(string.Format(items, "", "-- 请选择城市 --"));
                SQL = " AND DeletionStateCode = 0 AND ParentRegionNumber = '" + parentid + "' AND Layer=" + layer + " ORDER BY RegionName ASC";
                break;
            case "3":
                selOptions.Append(string.Format(items, "", "-- 请选择区县 --"));
                SQL = " AND DeletionStateCode = 0 AND ParentRegionNumber = '" + parentid + "' AND Layer=" + layer + " ORDER BY RegionName ASC";
                break;
        }

        DataTable dat = BLL.CommonBLL.Select(
            "vw_RegionBase",
            "RegionName, RegionNumber",
            SQL);

        foreach (DataRow dr in dat.Select(""))
        {
            selOptions.Append(string.Format(items, dr["RegionNumber"].ToString(), dr["RegionName"].ToString()));
        }

        dat.Clear();
        dat.Dispose();
        BLL.AjaxHelper.ResponseWrite(selOptions.ToString());
    }
    #endregion 
}
